﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace VehicleTrajectoryApp
{
    public partial class TextDYNASMARTToBinaryForm : Form
    {
        //private List<DYNASMARTVehicle> vehicleList;
        //private Dictionary<int, Node> nodeMap;
        //private List<MOEOutputSetting> MOESettingList;

        //private List<GroupedMOEOutputData> groupedResults;

        //private string pathStr;

        public TextDYNASMARTToBinaryForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = @"DYNASMART Vehicle File (*.dat) | *.dat";
            if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = @"DYNASMART Network File (*.dat) | *.dat";
            if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = @"Zone Mapping File (*.*) | *.*";
            if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox3.Text = openFileDialog1.FileName;
            }
        }

        private void textBox4_Leave(object sender, EventArgs e)
        {
            double min;
            if (!Double.TryParse(textBox4.Text, out min))
            {
                MessageBox.Show(this, "Invalid simulation start min!");                
                textBox4.Focus();
            }
            else
            {
                if (min < 0 || min > 1440)
                {
                    MessageBox.Show(this, "Invalid simulation start min!");
                    textBox4.Focus();
                }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox5.Text.Length > 0)
            {
                if (File.Exists(textBox5.Text))
                {
                    this.saveFileDialog1.FileName = textBox5.Text;
                }
            }
            if (this.saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox5.Text = this.saveFileDialog1.FileName;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string srcFileName = this.textBox1.Text;
            string networkFileName = this.textBox2.Text;
            string mappingFileName = this.textBox3.Text;
            float simulation_start_min = (float)(Double.Parse(this.textBox4.Text));
            string destFileName = this.textBox5.Text;

            if (srcFileName.Length <= 0)
            {
                MessageBox.Show(this, "The input text DYNASMART file is not specified!");
                return;
            }

            if (networkFileName.Length <= 0)
            {
                MessageBox.Show(this, "The DYNASMART network file is not specified!");
                return;
            }

            if (destFileName.Length <= 0)
            {
                MessageBox.Show(this, "The output binary DYNASMART file is not specified!");
                return;
            }

            Cursor = Cursors.WaitCursor;

            DYNASMARTVehicle.ConvertTextDYNASMARTVehicleFileToBinaryVehicleFile(srcFileName, destFileName, networkFileName, simulation_start_min, mappingFileName);
            MessageBox.Show(this, "Converting text vehicle file to binary vehicle file has finished!");

            Cursor = Cursors.Default;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
